package com.dhms.vostok1.utils

import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.dhms.mine.R
import com.dhms.mine.databinding.SelectMbrPopLayoutBinding
import com.dhms.vostok1.adapter.SelectPopAdapter
import com.dhms.vostok1.data.select_mbr.MultiItemInfo

class SelectMemberPop(activity: Activity, list: ArrayList<MultiItemInfo>) : PopupWindow() {

    init {
        val view = LayoutInflater.from(activity).inflate(R.layout.select_mbr_pop_layout, null)
        contentView = view
        val binding = DataBindingUtil.bind<SelectMbrPopLayoutBinding>(contentView)
        binding?.let {
            it.tvNum.text = String.format(activity.getString(R.string.select_num), list.size)
            it.confirm.setOnClickListener {
                dismiss()
            }
            it.rootView.setOnClickListener {
                dismiss()
            }
            it.recyclerview.layoutManager = LinearLayoutManager(activity)
            val adapter = SelectPopAdapter()
            it.recyclerview.adapter = adapter
            adapter.setNewInstance(list)
            adapter.setMemberSelectListener { t ->
                list.remove(t)
                adapter.notifyDataSetChanged()
                it.tvNum.text = String.format(activity.getString(R.string.select_num), list.size)
            }
        }
        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT
        isOutsideTouchable = true
        val dw = ColorDrawable(0x60000000)
        setBackgroundDrawable(dw)
        animationStyle = R.style.sign_pop_anim
    }
}